package controllers;

import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.SiteConfig;

import services.PageRankService;
import systems.ConfigSystem;

public class CheckPageRankServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		TreeMap<String, String> check = new TreeMap<String, String>();
		String name = (String) request.getParameter("domain");
		String pr = "";
		
		if (name == null || !UrlHelper.checkDomain(name)) {
			check.put("name", "Please provice your domain.");
		} else {
			name = UrlHelper.getMainDomain(name);
		} 
		
		
		if (check.size() == 0) {
			PageRankService prService = new PageRankService();
			
			pr = prService.getPR(URLDecoder.decode(name,"UTF-8"));
		}
		
		//Cache header
		response.addHeader("Cache-Control", "public, max-age=60000");
		
		request.setAttribute("check", check);
		request.setAttribute("name", name);
		request.setAttribute("pr", pr);
		try {
			request.getRequestDispatcher("/pages/check_page_rank.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
